package com.siwei.mes.service.experiment.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.experiment.ExperimentTested;
import com.siwei.mes.mapper.experiment.ExperimentTestedMapper;
import com.siwei.mes.service.experiment.ExperimentTestedService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 受检台账(ExperimentTested)表服务实现类
 *
 * @author linzi
 * @since 2024-07-26 16:58:47
 */
@Service
public class ExperimentTestedServiceImpl implements ExperimentTestedService {
    @Resource
    private ExperimentTestedMapper experimentTestedMapper;

    @Override
    public Long create(ExperimentTested entity) {
        entity.init(true);
        experimentTestedMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(ExperimentTested entity) {
        ExperimentTested experimentTested = experimentTestedMapper.selectByPrimaryKey(entity.getId());
        assertTrue(experimentTested == null, "受检台账信息不存在");
        entity.init(false);
        experimentTestedMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        ExperimentTested experimentTested = experimentTestedMapper.selectByPrimaryKey(id);
        assertTrue(experimentTested == null, "受检台账信息不存在");
        experimentTestedMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<ExperimentTested> getPage(PageQuery<ExperimentTested, ExperimentTested> query) {
        ExperimentTested searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> experimentTestedMapper.getList(searchParam));
    }

    @Override
    public ExperimentTested loadById(Long id) {
        return experimentTestedMapper.loadById(id);
    }

    @Override
    public List<ExperimentTested> getAll(ExperimentTested entity) {
        return experimentTestedMapper.getList(entity);
    }
}
